package com.ddone.util.mock;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ddone
 * @date 2024/5/15-10:04
 */
public class RandomDatetime {
    String dt;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public RandomDatetime(String dt) {
        this.dt = dt;
    }

    public Date getRandomDatetime(Integer hour) {
        int minute;
        if (hour.intValue() == 23) {
            minute = RandomNum.getRandInt(0, 30);
        } else {
            minute = RandomNum.getRandInt(0, 59);
        }
        int second = RandomNum.getRandInt(0, 59);
        String hourStr = StringUtils.leftPad(hour + "", 2, "0");
        String miniStr = StringUtils.leftPad(minute + "", 2, "0");
        String secStr = StringUtils.leftPad(second + "", 2, "0");
        String startTime = this.dt + " " + hourStr + ":" + miniStr + ":" + secStr;
        try {
            Date date = this.simpleDateFormat.parse(startTime);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("日期生成错误! ");
        }
    }
}
